home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 65.zip / BS1 part 65 / Chemistetics v2.00.adf / STV / source / search.c < prev    next >
C/C++ Source or Header  |  1991-01-09  |  6KB  |  213 lines

  1. /*********************************
  2. *  SEARCH  01/02/91
  3. *  Source file for STV
  4. *  © Copyright 1990 Timm Martin
  5. *  All Rights Reserved Worldwide
  6. **********************************/
  7.  
  8. #include <ctype.h>
  9. #include <exec/types.h>
  10. #include <functions.h>
  11. #include <intuition/intuition.h>
  12. #include <string.h>
  13. #include "func.h"
  14. #include "main.h"
  15.  
  16. /********************
  17. *  SEARCH REQUESTER
  18. *********************/
  19.  
  20. #define HOFFSET 16
  21. #define VOFFSET  8
  22. #define TOFFSET  8
  23.  
  24. struct IntuiText search_text =
  25. {
  26.   /* UBYTE              FrontPen  */ RED,
  27.   /* UBYTE              BackPen   */ BLUE,
  28.   /* UBYTE              DrawMode  */ JAM1,
  29.   /* SHORT              LeftEdge  */ HOFFSET,
  30.   /* SHORT              TopEdge   */ VOFFSET,
  31.   /* struct TextAttr *  ITextFont */ &text_attr,
  32.   /* UBYTE *            IText     */ (STRPTR)"Search for:",
  33.   /* struct IntuiText * NextText  */ NULL,
  34. };
  35.  
  36. #define INPUT_LENGTH 21
  37. UBYTE input_string[INPUT_LENGTH];
  38. UBYTE input_undo  [INPUT_LENGTH];
  39.  
  40. struct StringInfo input_info =
  41. {
  42.   /* UBYTE * Buffer     */ input_string,
  43.   /* UBYTE * UndoBuffer */ input_undo,
  44.   /* SHORT   BufferPos  */ 0,
  45.   /* SHORT   MaxChars   */ INPUT_LENGTH,
  46.   /* SHORT   DispPos    */ 0,
  47. };
  48.  
  49. struct Gadget input_gadget =
  50. {
  51.   /* struct Gadget *    NextGadget    */ NULL,
  52.   /* SHORT              LeftEdge      */ HOFFSET,
  53.   /* SHORT              TopEdge       */ LATER,
  54.   /* SHORT              Width         */ LATER,
  55.   /* SHORT              Height        */ LATER,
  56.   /* USHORT             Flags         */ GADGHCOMP,
  57.   /* USHORT             Activation    */ RELVERIFY,
  58.   /* USHORT             GadgetType    */ REQGADGET | STRGADGET,
  59.   /* APTR               GadgetRender  */ NULL,
  60.   /* APTR               SelectRender  */ NULL,
  61.   /* struct IntuiText * GadgetText    */ NULL,
  62.   /* LONG               MutualExclude */ NULL,
  63.   /* APTR               SpecialInfo   */ (APTR)&input_info,
  64.   /* USHORT             GadgetID      */ 1,
  65.   /* APTR               UserData      */ NULL,
  66. };
  67.  
  68. struct Requester search_req =
  69. {
  70.   /* Requester *        OlderRequest */ NULL,
  71.   /* SHORT              LeftEdge     */ LATER,
  72.   /* SHORT              TopEdge      */ LATER,
  73.   /* SHORT              Width        */ LATER,
  74.   /* SHORT              Height       */ LATER,
  75.   /* SHORT              RelLeft      */ 0,
  76.   /* SHORT              RelTop       */ 0,
  77.   /* struct Gadget *    ReqGadget    */ &input_gadget,
  78.   /* struct Border *    ReqBorder    */ NULL,
  79.   /* struct IntuiText * ReqText      */ &search_text,
  80.   /* USHORT             Flags        */ NULL,
  81.   /* UBYTE              BackFill     */ BLUE,
  82.   /* struct Layer *     ReqLayer     */ NULL,
  83. };
  84.  
  85. /* where to begin the search */
  86. search_line = 0;
  87.  
  88. /**********
  89. *  SEARCH
  90. ***********/
  91.  
  92. /*
  93. This procedure asks the user for a string to search and tries to find it.
  94. */
  95.  
  96. void search( void )
  97. {
  98.   BOOL finished;
  99.   int i;
  100.   struct IntuiMessage *imessage;
  101.  
  102.   input_gadget.TopEdge = VOFFSET + stats.Baseline + TOFFSET;
  103.   input_gadget.Width   = INPUT_LENGTH * stats.TextWidth;
  104.   input_gadget.Height  = stats.TextHeight;
  105.  
  106.   /* calculate size of search requester */
  107.   search_req.Width  = input_gadget.Width + 2 * HOFFSET;
  108.   search_req.Height = input_gadget.TopEdge + input_gadget.Height + VOFFSET;
  109.  
  110.   /* center the search requester in the window */
  111.   search_req.LeftEdge = (win->Width  - search_req.Width ) >> 1;
  112.   search_req.TopEdge  = (win->Height - search_req.Height) >> 1;
  113.  
  114.   /* if opened the search requester OK */
  115.   if (Request( &search_req, win ))
  116.   {
  117.     /* put cursor in gadget */
  118.     for (i = 0; i < 2000 && !(input_gadget.Flags & SELECTED); i++)
  119.       ActivateGadget( &input_gadget, win, &search_req );
  120.  
  121.     finished = NO;
  122.     while (!finished)
  123.     {
  124.       WAIT_FOR_INPUT;
  125.       /* only check for one message--the next message will be a REFRESHWINDOW
  126.        * which needs to be handled by the main loop
  127.        */
  128.       while (WINDOW_INPUT)
  129.       {
  130.         /* if pressed return, want to search */
  131.         if (imessage->Class == GADGETUP)
  132.           finished = YES;
  133.         ReplyMsg( (struct Message *)imessage );
  134.       }
  135.     }
  136.  
  137.     /* remove requester from window */
  138.     EndRequest( &search_req, win );
  139.  
  140.     /* if string is not blank */
  141.     if (input_string[0])
  142.     {
  143.       search_line = 0;
  144.       search_again();
  145.     }
  146.   }
  147. }
  148.  
  149. /****************
  150. *  SEARCH AGAIN
  151. *****************/
  152.  
  153. /*
  154. Given the specified search string, this procedure attempts to find that
  155. string from the current search line.
  156. */
  157.  
  158. void search_again( void )
  159. {
  160.   char *end;        /* end of file */
  161.   REG int i;        /* position in search string */
  162.   int length;       /* length of search string */
  163.   REG char *start;  /* position in text file */
  164.  
  165.   SET_POINTER;
  166.  
  167.   /* make sure still some file to search */
  168.   if (search_line < current_file.Lines)
  169.   {
  170.     /* find length of search string */
  171.     length = strlen( (char *)input_string );
  172.  
  173.     /* start at beginning of current search line and find end of file */
  174.     for (start = current_file.Table[search_line],
  175.          end = current_file.Buffer + current_file.FileSize - length;
  176.          start < end; start++ )
  177.     {
  178.       /* go until end of search string or until no match */
  179.       for (i = 0; tolower( input_string[i] ) == tolower( start[i] ) &&
  180.            i < length; i++);
  181.       /* If went all the way to end of string, must've matched.  Added
  182.        * search_line check just in case there are nulls in the file (e.g
  183.        * binary file) and search_line has gone past bounds.
  184.        */
  185.       if (i == length && search_line < current_file.Lines)
  186.       {
  187.         /* start display at current search line */
  188.         stats.Top = search_line;
  189.         /* make sure it can fit */
  190.         line_check();
  191.         /* redisplay at new position & adjust prop gadget */
  192.         text_display();
  193.         modify_vprop();
  194.         /* highlight search text */
  195.         text_string( (int)(search_line - stats.Top),
  196.                      (int)((long)start - (long)current_file.Table[search_line]),
  197.                      length );
  198.         /* begin searchin on next line */
  199.         search_line++;
  200.         CLEAR_POINTER;
  201.         return;
  202.       }
  203.       /* else check if sitting on newline; if so, now moved to next line */
  204.       else if (*start == '\0')
  205.         search_line++;
  206.     }
  207.   }
  208.  
  209.   /* if made it here, never found desired string */
  210.   DisplayBeep( win->WScreen );
  211.   CLEAR_POINTER;
  212. }
  213.